package com.wing.warehouse.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.warehouse.model.entity.WarehouseFactoryAreaEntity;
import com.wing.warehouse.model.entity.WarehouseFactoryAreaVO;
import com.wing.warehouse.model.request.WarehouseFactoryAreaForm;
import com.wing.warehouse.controller.admin.service.WarehouseFactoryAreaService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Validated
@Api(value = "Admin-仓库库区管理", tags = "#/warehouseFactory/warehouseFactoryArea")
@RestController("WarehouseFactoryAreaController")
@RequestMapping("warehouse/admin/warehouseFactoryArea")
@RequiredArgsConstructor
public class WarehouseFactoryAreaController {

    private final WarehouseFactoryAreaService warehouseAreaService;


    @ApiOperation(value = "查询所有仓库库区", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseFactoryAreaVO> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "999") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order,String name,String code,Long warehouseId){
        Page pg=new Page(page,limit);
        Map queryParam=new HashMap();
        queryParam.put("sort", StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("name",name);
        queryParam.put("code",code);
        queryParam.put("warehouseId",warehouseId);
        IPage<WarehouseFactoryAreaVO> list = warehouseAreaService.pageList4app(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加仓库库区", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(WarehouseFactoryAreaForm.Add.class) WarehouseFactoryAreaForm warehouseAreaForm) {
        System.out.println(1111);
        if (warehouseAreaService.add(warehouseAreaForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }


    @ApiOperation(value = "修改仓库库区", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(WarehouseFactoryAreaForm.Update.class) WarehouseFactoryAreaForm warehouseAreaForm) {
        WarehouseFactoryAreaEntity warehouseArea = warehouseAreaService.getById(warehouseAreaForm.getId());
        if(warehouseArea == null){
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(WarehouseFactoryAreaEntity.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(warehouseAreaForm, warehouseArea, copyOptions);
        if (warehouseAreaService.updateById(warehouseArea)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



    @ApiOperation(value = "删除仓库库区", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warehouseAreaId", value = "仓库库区id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long warehouseAreaId) {
        if (warehouseAreaService.removeById(warehouseAreaId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

    @ApiOperation(value="检查唯一性")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value="仓库库区名称", defaultValue = "", example = "plan-01", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value="仓库库区编码", defaultValue = "", example = "plan01", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "id",value = "仓库库区id",defaultValue = "", example = "1",required = false,dataType = "Long",paramType = "query")
    })
    @GetMapping("/isOnly")
    public JsonResult isOnly(@RequestParam(name="code",required = false) String code,@RequestParam(name = "name",required = false) String name,@RequestParam(name="id",required=false)Long id){
        QueryWrapper<WarehouseFactoryAreaEntity> queryWrapper=new QueryWrapper<>();
        if (StringUtils.isNotBlank(code)){
            queryWrapper.eq("code",code);
        }
        if (StringUtils.isNotBlank(name)){
            queryWrapper.eq("name",name);
        }
        if (id!=null) {
            queryWrapper.ne("id", id);
        }
        int count=warehouseAreaService.count(queryWrapper);
        return JsonResult.success(count==0);
    }
}
